#!/usr/bin/python3

import uapool
import zjl_util
import re
import os
import time
import requests
import getopt
import sys
import urllib


class TingShu8:
    """ download voice from ting89"""

    def __init__(self, u, d, offset):
        self.index_url = u
        self.dir = d
        self.start = offset
        if not os.path.exists(self.dir):
            os.makedirs(self.dir)
        uapool.UA()

    def get_file_info(self):
        """获取下载文件信息"""
        html = requests.get(self.index_url)
        # html.encoding = 'utf-8'
        doc = html.text.encode('latin-1').decode('gbk')
        # print(doc)
        l_pat = '<div class="ny_l">(.*?)</div>'
        r = re.compile(l_pat, re.S).findall(doc)
        print(r)
        print("------------------------------------------filelist")

        a_pat = "<a href=\'(.*?)\'>\[(.*?)\]</a>"
        r = re.compile(a_pat, re.S).findall(r[0])
        print(r)
        print(range(self.start, len(r)))
        for j in range(self.start, len(r)):
            print(j)
            i = r[j]
            file_name = i[1]
            item_url = i[0]
            if str(item_url).startswith('/'):
                item_url = uapool.get_base_url(self.index_url) + item_url
            html = requests.get(item_url)
            # html.encoding = 'gbk'
            # doc = html.text.encode('latin-1').decode('gbk')
            doc = html.text
            # print('############\n' + doc)
            audio_pat = '<iframe src="(.*?)".*?>.*?</iframe>'
            rt = re.compile(audio_pat, re.S).findall(doc)
            # print(r)
            iframe_url = rt[0]
            default_header = {
                "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
                "Accept-Encoding": "gzip, deflate, br",
                "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
                "Cache-Control": "max-age=0",
                "Connection": "keep-alive",
                "Cookie": "ASPSESSIONIDAARDCSAD=BJOHAIKBFEDDOPNKKNOMGIEM",
                "Host": "ai-w-ysts8.iiszg.com",
                "Referer": "https://www.ysts8.net/play_9064_52_1_1.html",
                "TE": "Trailers",
                "Upgrade-Insecure-Requests": "1",
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
            }
            # print(iframe_url)
            iframe_html = requests.get(iframe_url, headers=default_header)
            iframe_doc = iframe_html.text.encode('latin-1').decode('gb2312')
            if len(iframe_doc) == 0:
                print("异常重试")
                iframe_html = requests.get(iframe_url, headers=default_header)
                iframe_doc = iframe_html.text.encode('latin-1').decode('gb2312')
            if len(iframe_doc) == 0:
                print("异常重试2")
                iframe_html = requests.get(iframe_url, headers=default_header)
                iframe_doc = iframe_html.text.encode('latin-1').decode('gb2312')
            # print(iframe_doc)
            # mp3_pat = "mp3:'(.*?)\+'\+"
            mp3_pat = "setMedia\",\ \{.*?mp3:'(.*?)\r"
            r_mp3_url = re.compile(mp3_pat, re.S).findall(iframe_doc)
            # print('###############\n')
            # print(r_mp3_url)
            # print(len(r_mp3_url))
            # print('###############\n')
            if len(r_mp3_url) <= 0:
                print('#########异常内容：')
                print(iframe_doc)
                print(len(iframe_doc))
                iframe_html = requests.get(iframe_url, headers=default_header)
                iframe_doc = iframe_html.text.encode('latin-1').decode('gb2312')
                print(iframe_doc)
                exit()
            variable_pat = "\+(.*?)\+"
            variables = re.compile(variable_pat, re.S).findall(r_mp3_url[0])
            # print(variables)
            mp3_url = r_mp3_url[0]
            for variable in variables:
                variable_value_pat = variable + " = '(.*?)';"
                # 从iframe页面中获取变量值
                variable_value = re.compile(variable_value_pat, re.S).findall(iframe_doc)
                # print(variable + "\t" + str(variable_value))
                mp3_url = str(mp3_url).replace(variable, variable_value[0])
            # print("***************")
            mp3_url = mp3_url.replace('\'', '').replace('+', '')
            # print(mp3_url)
            # print("***************")
            mp3_split = mp3_url.split('?')
            # mp3_url = mp3_split[0]
            s = mp3_split[0].split('.')
            file_name = file_name + '.' + s[len(s) - 1]
            print('---------------(' + file_name + ', ' + mp3_url + ")---------------")
            host = mp3_url.replace("https://", "").replace("http://", "").split("/")[0]
            item_url_pat = '/play_(.*?)_(.*?)_(.*?)_(.*?).html'
            item_url_r = re.compile(item_url_pat, re.S).findall(item_url)[0]
            mp3_uri = mp3_url.replace("https://", "").replace("http://", "").replace(host + '/', '')
            jiidx = '/play_' + item_url_r[0] + '_' + item_url_r[1] + '_' + item_url_r[2] + '_' + str(int(item_url_r[3]) + 1) + '.html'
            jiids = '/play_' + item_url_r[0] + '_' + item_url_r[1] + '_' + item_url_r[2] + '_' + str(int(item_url_r[3]) - 1) + '.html'
            # referer = "https://ai-w-ysts8.iiszg.com/flw.asp?url=" + mp3_uri + "&jiidx=" + jiidx + "&jiids=" + jiids + "&id=" + \
            #           item_url_r[0] + "&ji=" + item_url_r[3] + "&said=" + item_url_r[1]
            referer_param = {'url': mp3_uri, 'jiidx': jiidx, 'jiids': jiids, 'id': item_url_r[0], 'ji': item_url_r[3], 'said':  item_url_r[1]}
            referer = "https://ai-w-ysts8.iiszg.com/flw.asp?" + urllib.parse.urlencode(referer_param)

            # referer = "https://ai-w-ysts8.iiszg.com/flw.asp?url=%D4%C1%D3%EF%D0%A1%CB%B5%2F%D4%C1%D3%EF%5F%B7%E2%C9%F1%D1%DD%D2%E5%2F001%2Emp3&jiidx=/play%5F15286%5F55%5F1%5F2%2Ehtml&jiids=/play%5F15286%5F55%5F1%5F0%2Ehtml&id=15286&ji=1&said=55"
            header = {
                "Accept": "*/*",
                "Accept-Encoding": "identity;q=1, *;q=0",
                "Accept-Language": "zh-CN,zh;q=0.9",
                "Connection": "keep-alive",
                "Host": host,
                "Referer": referer,
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0"
            }
            self.down_m4a(mp3_url, file_name, header)

    def down_m4a(self, file_url, file_name, header=None):
        """下载文件"""
        if header is None:
            header = {
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0",
                "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
                "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
                "Accept-Encoding": "gzip, deflate, br",
                "Connection": "keep-alive",
                "Upgrade-Insecure-Requests": "1"
            }
        i = 0
        redo_count = 0  # 用于记录下载出现错误是记录重试次数，顺利下载完后，次数归0
        print('down: ' + file_url)
        while redo_count < 5:
            file_path = self.dir + "/" + file_name
            try:
                zjl_util.download(file_url, file_path, header)
                return
            except Exception as e:
                print("request error url:" + file_url)
                print(e)
                print('-------\t重试下载  ' + file_url + '  \t-------')
                redo_count += 1
                time.sleep(redo_count * 30 + 1) # 每次重试增加时间
                # 提前结束本次循环，再次下载这一章
                if redo_count >= 5:
                    print('[error] 多次重试后失败')
                    exit()


def main():
    index_url = "https://www.ysts8.net/Yshtml/Ys14471.html"
    dir_path = "D:/data/楚留香_叶伟版"
    offset = 0
    opts, args = getopt.getopt(sys.argv[1:], "hu:d:", ["dir=", "offset="])
    for o, a in opts:
        if o == '-h':
            print("python3 down_ting89.py -u " + index_url + " -d " + dir_path + " --offset=" + str(offset))
            exit()
        if o == '-u':
            index_url = a
        if o == '-d':
            dir_path = a
        if o == '--offset':
            offset = int(a)
    print('目录地址：' + index_url)
    print('保存路径：' + dir_path)
    app = TingShu8(index_url, dir_path, offset)
    app.get_file_info()


if __name__ == '__main__':
    main()
